<?php

namespace App\Http\Controllers\Home\Hr;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Knowledge as KnowledgeModel;
use FileSystem;
use Knowledge;
use Tools;
use Auth;

class KnowledgeController extends Controller
{
    protected $passField = [
        'category','title','content','attachment','assign','status'
    ];

    public function knowledge(Request $request)
    {

        $between = Tools::getBetween(false,'Y-m-d');

        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $opt = [
            'status'=>['!=','-1']
        ];
        
        $list = Knowledge::getList($between,$page_info['skip'],$page_info['limit'],$opt);

        $count = Knowledge::getCount($between,$opt);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        foreach ($between as $k => $item){

            $between[$k] = date('Y-m-d',strtotime($item->toDateTimeString()));

        }

        return view('Home.User.Hr.Knowledge.knowledge',compact('list','page','num','between'));
    }

    public function knowledge_add(Request $request){
        if($request->isMethod('post')){
            
            //验证
            $this->validate(request(), [
                'category' => 'required|integer',
                'title' => 'required|string|max:100',
                'content' => 'required|string',
                'status' => 'required|string'
            ],[],[
                'category' => '分类',
                'title' => '标题',
                'content' => '内容',
                'status' => '状态',
            ]);

            //数据库操作
            $data = [];

            $file_id = intval(request()->input('file_id'));
            if($file_id){
                $data['attachment'] = $file_id;
            }

            foreach ($request->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    $data[$key] = $val;
                }
            }

            $data['assign'] = Auth::id();

            KnowledgeModel::create($data);

            return ['code'=>1];
            //返回结果
        }

        return view('Home.User.Hr.Knowledge.add');
    }

    public function knowledge_edit(KnowledgeModel $obj){
        if(request() ->isMethod('post')){

            //验证
            $this->validate(request(), [
                'category' => 'required|integer',
                'title' => 'required|string|max:100',
                'content' => 'required|string',
                'status' => 'required|string'
            ],[],[
                'category' => '分类',
                'title' => '标题',
                'content' => '内容',
                'status' => '状态',
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    $obj->$key = $val;
                }
            }

            $file_id = intval(request()->input('file_id'));
            if($file_id){
                $obj->attachment = $file_id;
            }

            $obj->save();

            //返回结果
            return ['code'=>1];
        }

        $obj->load('file');

        $obj->file_name = $obj->file?$obj->file->name:'';

        $obj->file_id = $obj->file?$obj->file->id:'';

        return view('Home.User.Hr.Knowledge.edit',compact('obj'));
    }

    public function knowledge_detail(KnowledgeModel $obj){

        $obj->load('file');

        $obj->file_name = $obj->file?$obj->file->name:'';

        switch ($obj->category){
            case 1;
                $obj->category_name = '资源分享';
                break;
            case 2;
                $obj->category_name = '案例分析';
                break;
            case 3;
                $obj->category_name = '市场检测报告';
                break;
            case 4;
                $obj->category_name = '人事制度';
                break;
            case 5;
                $obj->category_name = '福利制度';
                break;
            case 6;
                $obj->category_name = '顾问文档';
                break;
            case 7;
                $obj->category_name = '行政制度';
                break;
            case 8;
                $obj->category_name = '培训文档';
                break;
            case 9;
                $obj->category_name = '公司介绍';
                break;
            case 10;
                $obj->category_name = '入职培训';
                break;
            case 11;
                $obj->category_name = '技能培训';
                break;
            case 12;
                $obj->category_name = '经验分享';
                break;
            default:
                $obj->category_name = '资源分享';
        }
        
        return view('Home.User.Hr.Knowledge.detail',compact('obj'));
    }

    public function download(KnowledgeModel $obj){

        $fid = $obj->attachment;

        $res = FileSystem::downloadFile($fid,true);

        return response()->download($res['path'],$res['name']);

    }
    
    /*删除简历*/
    public function del_knowledge(KnowledgeModel $obj){

        if(Auth::user()->hasAnyRole(['admin']) || Auth::id()==$obj->uid){

            $obj->status = '-1';

            $res = $obj->save();

            if ($res){

                $rerurn = ['code'=>1,'msg'=>'OK'];

            }else{

                $rerurn = ['code'=>0,'msg'=>'服务器错误'];
            }
        }else{

            $rerurn = ['code'=>0,'msg'=>'没有权限'];
        }

        return $rerurn;
    }
    /*删除附件*/
    public function del_file_knowledge(KnowledgeModel $obj){

        if(Auth::user()->hasAnyRole(['admin']) || Auth::id()==$obj->uid){

            $obj->attachment = ' ';

            $res = $obj->save();

            if ($res){

                $rerurn = ['code'=>1,'msg'=>'OK'];

            }else{

                $rerurn = ['code'=>0,'msg'=>'服务器错误'];
            }
        }else{

            $rerurn = ['code'=>0,'msg'=>'没有权限'];
        }

        return $rerurn;
    }
}